package com.xuanxuan.socketdemo.adapter;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xuanxuan.socketdemo.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * author : zhouxuan77
 * e-mail : zhouxuan77@163.com
 * time   : 2022/04/03
 * desc   :
 * version: 1.0
 */
public class BluetoothDeviceAdapter extends RecyclerView.Adapter<BluetoothDeviceAdapter.BluetoothDeviceViewHolder> {

    private List<BluetoothDevice> listDevice;
    private List<String> listName;
    private List<String> listMac;

    public BluetoothDeviceAdapter(List<BluetoothDevice> listDevice, List<String> listName, List<String> listMac) {
        this.listDevice = listDevice;
        this.listName = listName;
        this.listMac = listMac;
    }

    @NonNull
    @Override
    public BluetoothDeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bluetooth_client_recycle_item, parent, false);
        return new BluetoothDeviceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BluetoothDeviceViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.getTv_title().setText(listName.get(position));
        holder.getTv_mac().setText(listMac.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(listDevice.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return listDevice.size();
    }

    class BluetoothDeviceViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_title;
        private TextView tv_mac;

        public BluetoothDeviceViewHolder(@NonNull View itemView) {
            super(itemView);

            tv_title = itemView.findViewById(R.id.tv_bluetooth_client_recycle_item_title);
            tv_mac = itemView.findViewById(R.id.tv_bluetooth_client_recycle_item_mac);
        }

        public TextView getTv_title() {
            return tv_title;
        }

        public TextView getTv_mac() {
            return tv_mac;
        }
    }

    private OnBluetoothDeviceAdapterClickListener listener;

    public interface OnBluetoothDeviceAdapterClickListener {
        void onClick(BluetoothDevice device);
    }

    public void setOnBluetoothDeviceAdapterClickListener(OnBluetoothDeviceAdapterClickListener listener) {
        this.listener = listener;
    }
}
